/**
 * 
 */
package com.apache.platform.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apache.api.api.ApiService;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.database.constant.SystemTools;
import com.apache.uct.common.ToolsUtil;
import com.apache.website.entity.Channel;
import com.apache.website.entity.Manuscript;
import com.apache.website.entity.ManuscriptVo;
import com.apache.website.manager.ChannelManager;
import com.apache.website.manager.ManuscriptManager;
import com.dwusoft.platform.template.FreemarkerHelper;

/**
 * @author dyhou
 */
@Controller
@RequestMapping("/artice/")
public class CreateArticeAction {
	private Logger log = Logger.getLogger(CreateArticeAction.class);
	@Autowired
	private ManuscriptManager manuscriptManager;
	@Autowired
	private ChannelManager channelManager;

	@Autowired
	private ApiService websiteService;

	@RequestMapping("publish.action")
	@ResponseBody
	public ResultMsg publishArtice(HttpServletRequest request) {
		ResultMsg msg = new ResultMsg("T", "发布成功");
		String filePath = SystemTools.getInstance().getValue("html_path", "UTF-8");
		String manuscriptId = request.getParameter("manuscriptId");
		ParamsVo vo = new ParamsVo();
		vo.setInfoId(manuscriptId);
		vo.setMethodKey("AllById");
		ManuscriptVo org = (ManuscriptVo) manuscriptManager.getInfoById(vo);
		ParamsVo cvo = new ParamsVo();
		cvo.setInfoId(org.getChannelId());
		Channel channel = (Channel) channelManager.getInfoById(cvo);
		Map<String, Object> data = new HashMap<String, Object>();
		Manuscript mt = (Manuscript) org;
		mt.setPublishedTime(System.currentTimeMillis());
		String content = org.getManuscriptContent();
		data.put("ctx", SystemTools.getInstance().getValue("portal_ctx"));
		data.put("manuscript", mt);
		data.put("channel", channel);
		data.put("manuscriptContent", content);
		if (ToolsUtil.isNotNull(mt.getUrl())) {
			String htmlPath = filePath + mt.getUrl();
			String ftlPath = "article.inc";
			if (ToolsUtil.isNotNull(channel.getTempName())) {
				ftlPath = channel.getTempName();
			}
			boolean flag = FreemarkerHelper.instance().createHtml(ftlPath, data, htmlPath);
			if (flag) {
				publishStatus(mt.getManuscriptId());
			} else {
				msg = new ResultMsg("F", "发布失败,请查看模板是否存在");
			}
		}

		log.info("发布稿件:" + JSONObject.fromObject(msg));

		return msg;
	}

	/**
	 * description:  修改信息状态
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private ResultMsg publishStatus(String manuscriptId) {
		Manuscript mt = new Manuscript();
		mt.setManuscriptId(manuscriptId);
		mt.setStatus("2");
		mt.setPublishedTime(System.currentTimeMillis());
		ParamsVo vo = new ParamsVo();
		vo.setObj(mt);
		boolean mark = manuscriptManager.editInfo(vo);
		ResultMsg msg = new ResultMsg("F", "发布失败");
		if (mark) {
			msg = new ResultMsg("T", "发布成功");
		}
		return msg;
	}

}
